package com.example.backend.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.entity.*;
import com.example.backend.mapper.CommentMapper;
import com.example.backend.mapper.LikeMapper;
import com.example.backend.mapper.PostMapper;
import com.example.backend.mapper.UserPostMapper;
import com.example.backend.service.PostService;
import com.example.backend.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.sql.Timestamp;
import java.util.Date;

/**
 * @Author Jcy
 * @Date 2022/5/3
 */
@Component
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private UserPostMapper userPostMapper;
    @Autowired
    private LikeMapper likeMapper;
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public int createPost(int userId, String content){
        Post post = new Post();
        UserPost userPost = new UserPost();
        post.setPost_content(content);
        Date date = new Date();
        Timestamp time = new Timestamp(date.getTime());
        post.setCreate_date(time);
        postMapper.insert(post);
        userPost.setUser_id(userId);
        userPost.setPost_id(post.getPost_id());
        userPostMapper.insert(userPost);
        return 0;
    }
    @Override
    public int like(int postId, int userId){
        Like like = new Like();
        if((likeMapper.getLikeCount(postId,userId) != null)){
            return 1;
        }
        like.setLike_user_id(userId);
        like.setLike_post_id(postId);
        Date date = new Date();
        Timestamp time = new Timestamp(date.getTime());
        like.setCreate_date(time);
        likeMapper.insert(like);
        return 0;
    }
    @Override
    public int comment(int userId, int postId, String comment){
        Comment comment1 = new Comment();
        if(comment.length()>250){
            return 1;
        }
        else {
            comment1.setComment_user_id(userId);
            comment1.setComment_post_id(postId);
            comment1.setComment_content(comment);
            Date date = new Date();
            Timestamp time = new Timestamp(date.getTime());
            comment1.setCreate_date(time);
            commentMapper.insert(comment1);
            return 0;
        }
    }
    @Override
    public int deletePost(int postId){
        if(postMapper.selectById(postId)==null) {
           return 1;
        }
        else {
            postMapper.deleteById(postId);
            userPostMapper.deletefromUserPost(postId);
            if(!likeMapper.getLikeId(postId).isEmpty()){
                likeMapper.deleteBatchIds(likeMapper.getLikeId(postId));
            }
            if(!commentMapper.getCommentId(postId).isEmpty()) {
                commentMapper.deleteBatchIds(commentMapper.getCommentId(postId));
            }
            return 0;
        }
    }
    @Override
    public int modifyPost(int postId, String content){
        Post post = postMapper.selectById(postId);
        if(postMapper.selectById(postId)==null){
            return 1;
        }
        else{
            post.setPost_content(content);
            postMapper.updateById(post);
            return 0;
        }
    }

    @Override
    public int deletePostByAdmin(int postId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("post_id", postId);
        if(postMapper.selectById(postId) == null || userPostMapper.selectOne(wrapper) == null){
            return 1;
        }
        postMapper.deleteById(postId);
        userPostMapper.deletefromUserPost(postId);
        if(!likeMapper.getLikeId(postId).isEmpty()){
            likeMapper.deleteBatchIds(likeMapper.getLikeId(postId));
        }
        if(!commentMapper.getCommentId(postId).isEmpty()) {
            commentMapper.deleteBatchIds(commentMapper.getCommentId(postId));
        }
        if(postMapper.selectById(postId) == null && userPostMapper.selectOne(wrapper) == null){
            return 0;
        }
        return 2;
    }
    @Override
    public PageUtils getPosts(int page, int pageSize){
        Page<PostVO> p = new Page<>(page, pageSize);
        p.setRecords(this.postMapper.getPost(p));
        return new PageUtils(p);
    }
    @Override
    public PageUtils getComments(int page, int pageSize){
        Page<CommentVO> p = new Page<>(page, pageSize);
        p.setRecords(this.commentMapper.getComments(p));
        return new PageUtils(p);
    }
    @Override
    public PageUtils getUserPosts(int userId,int page, int pageSize){
        Page<PostVO> p = new Page<>(page, pageSize);
        p.setRecords(this.postMapper.getUserPost(userId,p));
        return new PageUtils(p);
    }
}
